﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;

public enum TaskStateType
{
    UnStart,
    Runing,
    CurUtilFinish,
    WholeTaskFinish,
    BackMainPanel,
}

public class Task {

    private TaskType taskType = TaskType.TomatoTask;
    protected bool isRuning = false;
    protected TaskManager manager = null;
    protected int countDownSecond = 0;
    private DateTime start = DateTime.Now;
    private DateTime end = DateTime.Now;
    protected string customString = null;
    protected TaskStateType state = TaskStateType.UnStart;

    public TaskType GetTaskType()
    {
        return taskType;
    }

    public TaskStateType GetTaskStateType()
    {
        return this.state;
    }

    public Task(TaskManager manager,TaskType taskType)
    {
        this.manager = manager;
        this.taskType = taskType;
    }

    public virtual void Start()
    {
        isRuning = true;
        state = TaskStateType.Runing;
    }

    public virtual void TimeOver()
    {
        this.customString = "  [时间到]  ";
        state = TaskStateType.CurUtilFinish;
    }

    public virtual void BeginCountDown()
    {
        Timer.Instance.SetCallBack(RunFunc, 1f);

        CountDownPanel.Instance.SetRotateAngle(true);
        CountDownPanel.Instance.SetClickCountDownClock();
        CountDownPanel.Instance.SetDragClock();
        CountDownPanel.Instance.SetAutoSaveCustomValue();
        Timer.Instance.Run();
        start = DateTime.Now;
    }

    protected virtual void RunFunc(int passedSecond)
    {
        var remainSec = countDownSecond - passedSecond;
        if (remainSec <= 0)
        {
            CountDownPanel.Instance.SetCountDownTime(0, 0, 0f);
            this.TimeOver();
            this.Stop();
            this.manager.GoToNextTask();
        }
        else
        {
            CountDownPanel.Instance.SetCountDownTime((int)System.Math.Floor(remainSec / 60f), remainSec % 60, (float)remainSec / countDownSecond);
        }

        Debug.Log("remainSec :" + remainSec);
    }

    public virtual void Stop()
    {
        end = DateTime.Now;
        Timer.Instance.Stop();
        CountDownPanel.Instance.Close();
        isRuning = false;
    }

    public virtual bool IsRuning()
    {
       return this.isRuning; 
    }

    public virtual string GetLog()
    {
        StringBuilder build = new StringBuilder();
        build.Append("从：");
        build.Append(start.Hour);
        build.Append(":");
        build.Append(start.Minute);
        build.Append(":");
        build.Append(start.Second);

        build.Append("  到：");
        build.Append(end.Hour);
        build.Append(":");
        build.Append(end.Minute);
        build.Append(":");
        build.Append(end.Second);

        var t = end.Subtract(start);
        build.Append(" 历时： ");
        
        if (t.Minutes > 0 || t.Hours > 0)
        {
            var min = t.Minutes;
            if (t.Hours > 0)
            {
                min += t.Hours * 60;
            }

            build.Append(min);
            build.Append("分");
        }
        if (t.Seconds > 0)
        {
            build.Append(t.Seconds);
            build.Append("秒");
        }

        build.Append("  ");
        build.Append(taskType == TaskType.TomatoTask ? "[工作]" : "[休息]");
        

        if (!string.IsNullOrEmpty(customString))
        {
            build.Append(customString);
        }

        return build.ToString();
    }
}
